package com.danton.common;

import java.io.File;
import java.util.Map;

import com.danton.common.router.AuthRouter;
import com.danton.common.router.UserRouter;
import com.danton.model.BsAdminModel;
import com.danton.model.BsAdminRoleModel;
import com.danton.model.BsPermissionModel;
import com.danton.model.BsRoleModel;
import com.danton.model.BsRolePermissionModel;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class Config extends JFinalConfig {
	public static final String relative_path = File.separator + "upload" + File.separator;
	public static final String project = "";
	public static final String save_path = PathKit.getWebRootPath() + relative_path;
	public static boolean devMode = true;
	public static String accessFileName = "";

	public void configConstant(Constants me) {
		loadPropertyFile("config.properties");
		devMode = getPropertyToBoolean("devMode", true);
		me.setDevMode(devMode);
		me.setUrlParaSeparator("^");
		
		accessFileName = getProperty("accessFileName");
		PropKit.use(accessFileName);
	}

	public void configRoute(Routes me) {
		me.add(new UserRouter());
		me.add(new AuthRouter());
	}

	public void configPlugin(Plugins me) {
		Map<String, String> map = System.getenv();
		/**
		* 先从环境变量里面取，取不到再取配置文件
		*/
		String jdbcUrl = map.get("jdbcUrl");
		String user = map.get("user");
		String password = map.get("password");

		if(jdbcUrl==null){
		jdbcUrl = getProperty("jdbcUrl");
		user = getProperty("user");
		password = getProperty("password");
		}

		DruidPlugin dp = new DruidPlugin(jdbcUrl, user, password);
		me.add(dp);

		// ActiveRecordPlugin
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setShowSql(true);
		arp.addMapping("bs_admin", BsAdminModel.class);
		arp.addMapping("bs_role", BsRoleModel.class);
		arp.addMapping("bs_permission", BsPermissionModel.class);
		arp.addMapping("bs_admin_role", BsAdminRoleModel.class);
		arp.addMapping("bs_role_permission", BsRolePermissionModel.class);
		me.add(arp);

	}

	public void configInterceptor(Interceptors me) {
	}

	public void configHandler(Handlers me) {
	}

	@Override
	public void afterJFinalStart() {
		
		super.afterJFinalStart();
	}
}
